<?php 
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='DESC') { 
    $a = new ManageUserAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what=''){
    $l = new ManageUserAction();
    return $l->lookup($what);
}
function save($detail='',$id='',$action=''){
    $s = new ManageUserAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$action);
}

sajax_init();
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["uid","ID"],
                   ["login","User Name"],
                   ["fullname","First Name"],
                   ["privilege","Privilege Type"],
                   ["privilege_level","Privilege Level"],
                   ["created_by","Created By"],
                   ["creation_date","Creation Date"]
		  	
                 ]
    var  extra = [ [true,'uid','edit'],
                   [true,'uid','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable(data[0],fields, extra)
}

function privilege_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('privilege'), 'id','usertype_name')
}
function privilegelevel_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('privilege_level'), 'id','usertypelevel_name')
}
function save () {
    data = $('form2').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    x_save(data,id, function(new_data1){

      Json_eval(new_data1);
      filter();
      show_t()
    });
}
function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
        $('altfrm').value = "New User"
    } else {
        hide_t('thetable','newform')
        $('altfrm').value = "Back to List"
    }
    $('form2').reset();
}

function edit (id) {
    x_save('',id,'get', function(new_data1){
       d = Json_eval(new_data1);
       $('user_name').value = d[0]['login'];
       $('user_password').value = d[0]['password'];
       $('password_confirm').value = d[0]['password'];
       $('first_name').value = d[0]['firstname'];
       $('last_name').value = d[0]['lastname'];
       $('myid').value = d[0]['id'];
       optionSelected($('privilege'), d[0]['privilege'])
       optionSelected($('privilege_level'), d[0]['privilege_level'])
       hide_t('thetable','newform');
    }) 
    return false;
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}
function integ() {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('','','integ', function(new_data1){
        Json_eval(new_data1);
       // filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}
function set_stat(){
  return true
}

function check_input(){
  name_re = /^\b[\w\d\s]{1,25}\b/gim;
  username_re = /^\b[A-Za-z\d]{4,25}\b/gim;
  password_re = /^\b[\w\d]{4,25}\b/gim;
  
  var str_last_name = $('last_name').value;
  var str_first_name = $('first_name').value;
  var str_user_name = $('user_name').value;
  var str_user_password = $('user_password').value;
  var str_password_confirm = $('password_confirm').value;
  
  var obj_last_name_check = document.getElementById('last_name_check');
  var obj_first_name_check = document.getElementById('first_name_check');
  var obj_user_name_check = document.getElementById('user_name_check');
  var obj_user_password_check = document.getElementById('user_password_check');
  var obj_password_confirm_check = document.getElementById('password_confirm_check');
  
  ar_last_name = str_last_name.match(name_re);
  ar_first_name = str_first_name.match(name_re);
  ar_user_name = str_user_name.match(username_re);
  ar_user_password = str_user_password.match(password_re);
  ar_password_confirm = str_password_confirm.match(password_re);
    
  if((ar_last_name) && (ar_last_name[0] == str_last_name)) { obj_last_name_check.innerHTML = "PASSED"; } else { obj_last_name_check.innerHTML = "FAILED"; }
  if((ar_first_name) && (ar_first_name[0] == str_first_name)) { obj_first_name_check.innerHTML = "PASSED"; } else { obj_first_name_check.innerHTML = "FAILED"; }
  if((ar_user_name) && (ar_user_name[0] == str_user_name)) { obj_user_name_check.innerHTML = "PASSED"; } else { obj_user_name_check.innerHTML = "FAILED"; }
  if((ar_user_password) && (ar_user_password[0] == str_user_password)) { obj_user_password_check.innerHTML = "PASSED"; } else { obj_user_password_check.innerHTML = "FAILED"; }
  if((ar_password_confirm) && (ar_password_confirm[0] == str_password_confirm)) { obj_password_confirm_check.innerHTML = "PASSED"; } else { obj_password_confirm_check.innerHTML = "FAILED"; }
  
 document.getElementById('AddUserSubmit').disabled = true;
  
  if(obj_last_name_check.innerHTML == "PASSED" 
    && obj_first_name_check.innerHTML == "PASSED" 
    && obj_user_name_check.innerHTML == "PASSED" 
    && obj_user_password_check.innerHTML == "PASSED" 
    && obj_password_confirm_check.innerHTML == "PASSED" 
    && str_user_password == ar_password_confirm)
  {
   document.getElementById('AddUserSubmit').disabled = false;
  }
  
}

</script>
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<!--<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/theme2.css" type="text/css" />-->
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%">
            <div class="pathway"><font size="2px" color="#000033"><b><?=$config['version']?></b></font></div>       
        </td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="#">
                        <img src="<?=WEB_MODULE_DIR?>images/help_f2.png" 
                             align="middle" name="new" border="0"><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">
        <table class="adminheading">
            <tr>
                <th class="cpanel" rowspan="2" nowrap>User Management&nbsp;<font size="1px" color="#CCCCCC">[<?=strtoupper($auth['privilege'])?>]</font></th>
            </tr>
        </table>
    </div>
        <table class="adminlist">
          <tr>
            <th class="title"><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></th>
          </tr>
          <tr>
            <td width="40%" valign="top">
            <!-- filter panel -->
		  <div id="panel">
		    <table class="adminlist">
		      <tr>
		        <td>
			  <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
			    <ul>
			      <li>
			        <span>
			          <select class="field select" style="width:10em" name="search[searchfield]">
		                    <option value="login">User Name</option>
                                    <option value="firstname">First Name</option>
                                    <option value="lastname">Last Name</option>
			          </select>
                                </span>

                                <span>
                                  <input class="field text" size="15" name="search[searchtext]" value="" onKeyup="filter()" />
                                </span>

                                <span>
                                  <input id="saveForm" class="btTxt" type="button" value="Search" onClick="filter()"/>
                                  <input class="btTxt" type="button" value="Clear" onClick="this.form.reset();filter()"/>
                                  <input id="altfrm" class="btTxt" type="button" value="New User" onClick="show_t()"/>
 <!--                                 <input id="altfrm" class="btTxt" type="button" value="Integrate" onClick="integ()"/>-->
                                </span>
                              </li>
                            </ul>
                          </form>
			</td>
		      </tr>
		    </table>
		  </div>
			<!-- filter panel end -->
		</td>
	    </tr>
	</table>
        <table class="adminform">
            <tr>
                <th class="title">:: SEARCH RESULTS</th>
            </tr>
            <tr>
              <td>
                      <div id="thetable">
                        <img src="/mcs/modpub/images/loading_small.gif"/>
                      </div>
              </td>
	    </tr>
        </table>
        <table> 
            <tr>
              <td align="center">
                <div id="newform" style="display: none" class="container">
                  <h1>New</h1>
                        <form id="form2" class="wufoo" method="post" onSubmit="return false;">
                          <input type="hidden" value="" id="myid" />
                            <div class="info">
                              <h2>New User</h2>
                                <p>Create New User</p>
                            </div>
                              <ul>
                                <table>
                                  <tr>
                                    <td class="data">USER NAME:</td>
                                    <td class="data">
                                     <input type="text" id="user_name" name="details[login]" onkeyup="check_input()" maxlength="25">
                                    </td>
                                    <td class="data" width="40px">
                                      <span class="_check" id="user_name_check">FAILED</span>
                                    </td>             
                                  </tr>
                                  </tr>
                                  <tr>
                                    <td class="data">PASSWORD:</td>
                                    <td class="data">
                                      <input type="password" id="user_password" name="details[password]" onkeyup="check_input()" maxlength="25">
                                    </td>
                                    <td class="data">
                                      <span class="_check" id="user_password_check">FAILED</span>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td class="data">CONFIRM PASSWD:</td>
                                    <td class="data">
                                      <input type="password" id="password_confirm" onkeyup="check_input()" maxlength="25"> 
                                    </td>
                                    <td class="data">
                                      <span class="_check" id="password_confirm_check">FAILED</span>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td class="data">LAST NAME:</td>
                                    <td class="data">
                                      <input type="text" id="last_name" name="details[lastname]" onkeyup="check_input()" maxlength="25">
                                    </td>
                                    <td class="data">
                                      <span class="_check" id="last_name_check">FAILED</span>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td class="data">FIRST NAME:</td>
                                    <td class="data">
                                      <input type="text" id="first_name" name="details[firstname]" onkeyup="check_input()" maxlength="25">
                                    </td>
                                    <td class="data">
                                      <span class="_check" id="first_name_check">FAILED</span>
                                    </td>
                                  </tr>
                              <tr>
                                <td><b>Privilege Type</b></td>
                                <td>
                              <!--   <select disabled="yes" class="field select" style="width:4" id="en_status" name="enrollment[en_status]">-->
                                  <select  class="field select" style="width:4" id="privilege" name="details[privilege]">
                                  </select>
                                </td>
                              </tr>
                              <tr>
                                <td><b>Privilege Level</b></td>
                                <td>
                              <!--   <select disabled="yes" class="field select" style="width:4" id="en_status" name="enrollment[en_status]">-->
                                  <select  class="field select" style="width:4" id="privilege_level" name="details[privilege_level]">
                                  </select>
                                </td>
                              </tr>
                                </table>
                                <li>
                                  <span>
                                    <input id="AddUserSubmit" name="submit" class="btTxt" type="button" value="Save" onClick="save();"/>
                                    <input id="saveForm" class="btTxt" type="reset" value="Clear" onClick=""/>
                                    <input id="saveForm" class="btTxt" type="button" value="Cancel" onClick="hide_t('newform', 'thetable')"/>
                                  </span>
                                </li>
                              </ul>
                        </form>
                    </div>
              </td>
            </tr>
        </table>
<script type="text/javascript">
x_getlist(getlist_cb)
x_lookup('privilege',privilege_cb)
x_lookup('privilege_level',privilegelevel_cb)
</script>

<?php require_once('footer.php'); ?>
